#include "serialportform.h"
#include "ui_serialportform.h"

#include <QSerialPortInfo>

SerialPortForm::SerialPortForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::SerialPortForm)
    , m_serial(nullptr)
{
    ui->setupUi(this);

    IsBreakConnect = false;

    connect(ui->pushButton_open, &QPushButton::clicked, this, &SerialPortForm::slot_pushButton_open_clicked);

    /**
     * 系统自动生成
     */
    // connect(ui->pushButton_flush, &QPushButton::clicked, this, &SerialPortForm::on_pushButton_flush_clicked);

    // connect(ui->comboBox_port, &QComboBox::currentIndexChanged, this, &SerialPortForm::on_comboBox_port_currentIndexChanged);
    // connect(ui->comboBox_baund, &QComboBox::currentIndexChanged, this, &SerialPortForm::on_comboBox_baund_currentIndexChanged);


    QList<QSerialPortInfo> lists = QSerialPortInfo::availablePorts();
    for(int i=0;i<lists.count();i++){
        ui->comboBox_port->addItem(lists[i].portName());
    }
    ui->comboBox_port->setCurrentIndex(0);
}

SerialPortForm::~SerialPortForm()
{
    delete ui;
}


void SerialPortForm::on_comboBox_port_currentIndexChanged(int index)
{
    Q_UNUSED(index)
    this->m_portName = ui->comboBox_port->currentText();
}


void SerialPortForm::on_comboBox_baund_currentIndexChanged(int index)
{
    Q_UNUSED(index)
    this->m_baundRate = ui->comboBox_baund->currentText().toInt();
}

#include <QMessageBox>
void SerialPortForm::slot_pushButton_open_clicked()
{
    if(IsBreakConnect){
        ui->pushButton_open->setText("打开串口");

        QMessageBox msgBox;
        msgBox.setText("串口已经关闭");
        msgBox.setInformativeText("提示");
        msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
        msgBox.exec();

        IsBreakConnect = false;
        return;
    }

    if(!m_serial){
        m_serial = new QSerialPort();
        m_serial->setPortName(ui->comboBox_port->currentText());
        m_serial->setBaudRate(ui->comboBox_baund->currentText().toInt());
        m_serial->setDataBits(QSerialPort::Data8);
        m_serial->setStopBits(QSerialPort::OneStop);
        m_serial->setParity(QSerialPort::NoParity);
        if(m_serial->open(QIODevice::ReadWrite)){
            connect(m_serial, &QSerialPort::readyRead, this, [=](){
                QByteArray array = m_serial->readAll();
                emit SigData(array);
            });
            IsBreakConnect = true;
            ui->pushButton_open->setText("关闭串口");

            QMessageBox msgBox;
            msgBox.setText("打开串口成功");
            msgBox.setInformativeText("提示");
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
            msgBox.exec();
        }else{
            QMessageBox msgBox;
            msgBox.setText("打开串口失败");
            msgBox.setInformativeText("提示");
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
            msgBox.exec();
        }
    }
}



void SerialPortForm::on_pushButton_flush_clicked()
{
    ui->comboBox_port->clear();
    QList<QSerialPortInfo> lists = QSerialPortInfo::availablePorts();
    for(int i=0;i<lists.count();i++){
        ui->comboBox_port->addItem(lists[i].portName());
    }
    ui->comboBox_port->setCurrentIndex(0);
}


void SerialPortForm::Send(const stParameter& parameter)
{
    this->m_serial->write(parameter.Array);
}
